package cn.hejiang.mamis.core.config;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.config.Resource;
import org.ssssssss.magicapi.core.context.MagicUser;
import org.ssssssss.magicapi.core.exception.MagicAPIException;
import org.ssssssss.magicapi.core.exception.MagicLoginException;
import org.ssssssss.magicapi.core.interceptor.AuthorizationInterceptor;
import org.ssssssss.magicapi.core.model.MagicEntity;
import org.ssssssss.magicapi.core.resource.DatabaseResource;
import org.ssssssss.magicapi.core.service.MagicResourceService;
import org.ssssssss.magicapi.core.service.MagicResourceStorage;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;

import cn.hejiang.mamis.core.entity.PlatUser;
import cn.hejiang.mamis.core.entity.SessionDetails;
import cn.hejiang.mamis.core.magic.MagicResourceServiceImpl;
import cn.hejiang.mamis.core.service.ContextService;
import cn.hejiang.mamis.core.service.PlatSysService;

/**
 * 引入租户/系统之后，数据源和存储需要自定义实现，进行隔离
 *
 */
@Configuration
public class MagicApiConfig {
	@Bean
	@ConditionalOnMissingBean
	public MagicResourceService magicResourceService(
			ObjectProvider<List<MagicResourceStorage<? extends MagicEntity>>> magicResourceStoragesProvider,
			PlatSysService platSysService, ContextService contextService, ApplicationContext applicationContext,
			org.ssssssss.magicapi.core.resource.Resource workspace) {
		return new MagicResourceServiceImpl(workspace, magicResourceStoragesProvider.getObject(), applicationContext,
				platSysService, contextService);
	}

	@Bean
	public MagicDynamicDataSource magicDynamicDataSource() {
		MagicDynamicDataSource dynamicDataSource = new MagicDynamicDataSource();
		return dynamicDataSource;
	}

	@Bean
	@ConditionalOnProperty(prefix = "magic-api", name = "resource.type", havingValue = "database")
	public org.ssssssss.magicapi.core.resource.Resource magicDatabaseResource(MagicAPIProperties properties,
			DataSource dataSource) {
		Resource resourceConfig = properties.getResource();
		if (dataSource == null) {
			throw new MagicAPIException("当前未配置数据源，如已配置，请引入 spring-boot-starter-jdbc 后在试!");
		}
		return new DatabaseResource(new JdbcTemplate(dataSource), resourceConfig.getTableName(),
				resourceConfig.getPrefix(), resourceConfig.isReadonly());
	}

	@Bean
	public AuthorizationInterceptor authorizationInterceptor(ContextService session) {
		return new AuthorizationInterceptor() {
			@Override
			public boolean requireLogin() {
				return true;
			}

			@Override
			public MagicUser getUserByToken(String token) throws MagicLoginException {
				SessionDetails details = session.getSession(null);
				if (details != null) {
					PlatUser user = details.getCurUser();
					return new MagicUser(user.getId(), user.getName(), "");
				}

				return null;
			}
		};
	}
}
